package com.briup.mr;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student  implements DBWritable, WritableComparable<Student> {
    private String id;
    private String name;
    private String className;
    private int  sumScore;
    private  int  avgScore;



    @Override
//    满足序列化 将对象的所有属性写出
    public void write(DataOutput out) throws IOException {
        out.writeUTF(id);
        out.writeUTF(name);
        out.writeUTF(className);
        out.writeInt(sumScore);
        out.writeInt(avgScore);

    }

    @Override
//    满足反序列化 从流中读取到所有的属性
    public void readFields(DataInput in) throws IOException {
        id=in.readUTF();
        name=in.readUTF();
        className=in.readUTF();
        sumScore=in.readInt();
        avgScore=in.readInt();

    }

    @Override
//    写出当前对象的属性到数据库
//    insert into student(id,name,class,sumScore,avgScore) values(?,?,?,?,?)
    public void write(PreparedStatement ps) throws SQLException {
        ps.setString(1,id);
        ps.setString(2,name);
        ps.setString(3,className);
        ps.setInt(4,sumScore);
        ps.setInt(5,avgScore);

    }

    @Override
//    从数据查询结果集获取属性值
//    select id,name,class,sumScore,avgScore from student
    public void readFields(ResultSet resultSet) throws SQLException {
       id=resultSet.getString(1);
       name=resultSet.getString("name");
       className=resultSet.getString("class");
       sumScore=resultSet.getInt(4);
       avgScore=resultSet.getInt(5);


    }

    @Override
//    自然排序必须实现的方法 后人那个当前类所有的对象 都具备比较性
    public int compareTo(Student o) {
        return o.id.compareTo(this.id);
    }
}
